﻿Imports System.IO
Imports Infragistics.Win.AppStyling
Imports Infragistics.Win.AppStyling.Runtime
Imports System.Windows.Forms

Public Class dlgChangeTheme

    ''' <summary>
    ''' Hàm tự load Style file vào cboStyle
    ''' </summary>
    ''' <param name="comboBox">ComboBox dùng để hiển thị tên các file Style</param>
    ''' <param name="fromDirectory">Đường dẫn chứa các file style</param>
    ''' <remarks>Nguyễn Huy Thường added on 20/06/2012</remarks>
    Private Sub LoadComboWithStyleLibraryFiles(ByVal comboBox As ComboBox, ByVal fromDirectory As DirectoryInfo)
        If (fromDirectory Is Nothing) Then
            Exit Sub
        End If
        Dim styleLibraryFiles() As FileInfo = fromDirectory.GetFiles("*.isl")
        Dim styleLibraryFile As FileInfo
        Dim i As Integer = 0
        Dim defaultIndex As Integer = 0
        For Each styleLibraryFile In styleLibraryFiles
            comboBox.Items.Add(styleLibraryFile)
            If styleLibraryFile.Name = clscommon.DecodingData(My.Settings.Crrthm) Then
                defaultIndex = i
            End If
            i += 1
        Next
        cboStyle.SelectedIndex = defaultIndex
    End Sub

    ''' <summary>
    ''' Thuộc tính lưu đường dẫn đến thư mục chứa các file style
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 20/06/2012</remarks>
    Private ReadOnly Property StyleLibrariesDirectory() As DirectoryInfo
        Get
            Dim di As DirectoryInfo = New DirectoryInfo(System.IO.Path.GetDirectoryName(Application.ExecutablePath))
            Dim styleLibrariesDir() As DirectoryInfo = di.GetDirectories("Style")
            If (styleLibrariesDir.Length > 0) Then
                Return styleLibrariesDir(0)
            End If
            Return Nothing
        End Get
    End Property

    Private Sub btnAccept_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
        Try
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    Private Sub dlgChangeTheme_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cboStyle.Items.Clear()
            LoadComboWithStyleLibraryFiles(cboStyle, StyleLibrariesDirectory)
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    Private Sub cboStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStyle.SelectedIndexChanged
        Try
            Dim selectedStyleLibrary As FileInfo = Nothing
            If (TypeOf cboStyle.SelectedItem Is FileInfo) Then
                selectedStyleLibrary = cboStyle.SelectedItem
            End If
            StyleManager.Load(selectedStyleLibrary.FullName)
            My.Settings.Crrthm = clscommon.EncodingData2(selectedStyleLibrary.Name)
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
End Class
